package org.intertwine.rssbot;

import org.jibble.pircbot.PircBot;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.Collection;

/**
 * Created by IntelliJ IDEA.
 * User: Stian Lindhom
 * Date: 21.feb.2010
 * Time: 14:43:21
 * To change this template use File | Settings | File Templates.
 */
public class PingBot extends PircBot {

    private String[] channels;
    private FeedManager fm;
    private String master;


    public PingBot(FeedManager fm, String nick, String login, String channels, String masterUser) {
        this.fm = fm;
        this.channels = channels.split(",");
        this.setName(nick);
        this.setLogin(login);
        this.master = masterUser;
    }    

    @Override
    protected void onDisconnect() {
        fm.save();
    }

    @Override
    protected void onPrivateMessage(String sender, String login, String hostname, String message) {
        String[] messages = message.split(" ");
        String[] commands = Arrays.copyOfRange(messages, 0, messages.length-1);
        if (commands.length == 0) {
            this.sendMessage(sender, "Not enough arguments specified. Did you forget to specify channel?");
            return;
        }
        String channel = messages[messages.length-1];
        handleMessage(channel, sender, login, hostname, commands);
    }

    @Override
    protected void onMessage(String channel, String sender, String login, String hostname, String message) {
        String[] commands = message.split(" ");
        handleMessage(channel, sender, login, hostname, commands);
    }

    private void handleMessage(String channel, String sender, String login, String hostname, String[] commands) {
        String user = login + "@" + hostname;

        String command = commands[0];
         if (command.startsWith("!addfeed")) {
             if (checkUser(sender, user)) return;
             if (commands.length == 2) {
                 addFeed(sender, commands[1]);
             } else if (commands.length == 4) {
                 addFeed(sender, commands[1], commands[2], commands[3]);
             }
         } else if (command.startsWith("!removefeed")) {
             if (checkUser(sender, user)) return;
             removeFeed(sender, commands[1]);
         } else if (command.startsWith("!listfeeds")) {
             if (checkUser(sender, user)) return;
             listFeeds(sender);
         } else if (command.startsWith("!quit")) {
             if (checkUser(sender, user)) return;
             quit();
         }
    }

    private boolean checkUser(String sender, String user) {
        if (!user.equals(master)) {
            this.sendNotice(sender, "Sorry " + sender + ", I can not let you do that.");
            return true;
        }
        return false;
    }

    private void quit() {
        this.quitServer();
    }

    private void listFeeds(String sender) {
        Collection<Feed> feeds = fm.listFeeds();
        if (feeds.isEmpty()) {
             this.sendNotice(sender, "There are no feeds registered.");
        }
        int i = 1;
        for (Feed feed : feeds) {
            this.sendNotice(sender, (i++) + ". " + feed.getFeedUrl());
        }
    }

    private void removeFeed(String sender, String feedUrl) {
        fm.removeFeed(feedUrl);
        this.sendMessage(sender, "Feed for " + feedUrl + " removed.");
    }

    private void addFeed(String channel, String url) {
        Feed f = new Feed(url);
        addFeed(channel, f);
    }

    private void addFeed(String channel, String url, String username, String password) {
        Feed f = new Feed(url, username, password);
        addFeed(channel, f);
    }

    private void addFeed(String sender, Feed f) {
        if (f != null) {
           fm.addFeed(f);
           this.sendNotice(sender, "Feed for " + f.getFeedUrl() + " added.");
        }
    }

    @Override
    protected void onConnect() {
        super.onConnect();
        for (String channel : channels) {
            this.joinChannel(channel);
        }
    }
}
